package top.hzy520.common.rest;

import top.hzy520.common.constants.Constants;
import top.hzy520.user.entity.UserElement;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: HouZeYu
 * @Description:
 * @Date: Created in 21:00 2018/6/12
 */
public class BaseController {
    /**
     *@Author: HouZeYu
     *@Description: 通过redis中的session获取当前用户
     *@Date: 21:34 2018/6/3
     */
    public UserElement getCurrentUser(HttpServletRequest request){
        if (request.getSession().getAttribute(Constants.REQUEST_USER_SESSION)!=null){
            UserElement userElement= (UserElement)request.getSession().getAttribute(Constants.REQUEST_USER_SESSION);
            return userElement;
        }
        return null;
    }
    /**
    *@Author: HouZeYu
    *@Description: 得到设备ip
    *@Date: 21:00 2018/6/12
    */
    public String getIpFromRequest(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if(ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip.equals("0:0:0:0:0:0:0:1")?"127.0.0.1":ip;
    }
}
